package com.hqyj.crmdemo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.crmdemo.entity.Custmer;
import com.hqyj.crmdemo.entity.CustomerLoss;
import com.hqyj.crmdemo.entity.RestResult;
import com.hqyj.crmdemo.entity.SaleChance;
import com.hqyj.crmdemo.service.ICustomerLossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何万林
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/customerLoss")
@CrossOrigin
public class CustomerLossController {
    @Autowired
    ICustomerLossService service;

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResult<CustomerLoss> queryPage(@RequestParam(defaultValue = "1")@PathVariable("page") Integer page,
                                            @RequestParam(defaultValue = "7")@PathVariable("size") Integer size){
        Page<CustomerLoss> page1 = service.page(new Page<>(page, size));
        Long total = page1.getTotal();

        return  RestResult.ok(page1.getRecords(),total);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public RestResult<CustomerLoss> queryAll() {
        List<CustomerLoss> customerLosses = service.queryAll();
        return RestResult.ok(customerLosses);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public RestResult<CustomerLoss> add(CustomerLoss customerLoss) {
        Boolean aBoolean = service.add(customerLoss);
        if (aBoolean) {
            return RestResult.ok("success");
        }else{
            return RestResult.ok("false");
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public RestResult<CustomerLoss> delete(Integer lossId) {
        Boolean aBoolean = service.delete(lossId);
        if (aBoolean) {
            return RestResult.ok("success");
        }else{
            return RestResult.ok("false");
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public RestResult<CustomerLoss> update(CustomerLoss customerLoss) {
        Boolean aBoolean = service.update(customerLoss);
        if (aBoolean) {
            return RestResult.ok("success");
        }else{
            return RestResult.ok("false");
        }
    }
}
